<?php

/**
 * This is the model class for table "{{imgs}}".
 *
 * The followings are the available columns in table '{{imgs}}':
 * @property integer $aid
 * @property string $title
 * @property string $content
 * @property string $img
 * @property string $url
 * @property integer $cid
 * @property integer $type
 * @property integer $order
 * @property integer $enabled
 * @property integer $uid
 * @property string $longitude
 * @property string $latitude
 * @property integer $ctime
 * @property integer $utime
 */
class Imgs extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Imgs the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{imgs}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title', 'required'),
			array('cid, type, order, enabled, uid, ctime, utime', 'numerical', 'integerOnly'=>true),
			array('title, img, url', 'length', 'max'=>255),
			array('longitude, latitude', 'length', 'max'=>20),
			array('content', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('aid, title, content, img, url, cid, type, order, enabled, longitude, latitude, ctime, utime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'username' => array(self::HAS_ONE, 'Users', 'uid'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
            'aid' => 'Aid',
            'title' => '说明',
            'content' => '内容',
            'img' => '图片',
            'url' => '链接',
            'cid' => '分类',
            'type' => '类型',
            'order' => '排序',
            'enabled' => '显示',
			'uid' => '用户',
			'longitude' => '经度',
			'latitude' => '纬度',
			'ctime' => '上传时间',
			'utime' => '更新时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
        if(empty($this->uid) && (!Yii::app()->user->isSuperuser)){
            $this->uid = Yii::app()->user->id;
        }

		$criteria->compare('aid',$this->aid);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('img',$this->img,true);
		$criteria->compare('url',$this->url,true);
		$criteria->compare('cid',$this->cid);
		$criteria->compare('type',$this->type);
		$criteria->compare('order',$this->order);
		$criteria->compare('enabled',$this->enabled);
		$criteria->compare('uid',$this->uid);
		$criteria->compare('longitude',$this->longitude,true);
		$criteria->compare('latitude',$this->latitude,true);
		$criteria->compare('ctime',$this->ctime);
		$criteria->compare('utime',$this->utime);

		return new CActiveDataProvider($this, array(
            'sort'=>array('defaultOrder'=>'t.ctime DESC'),
			'criteria'=>$criteria,
		));
	}

    public function search_imei()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;
        if(empty($this->uid) && (!Yii::app()->user->isSuperuser)){
            $this->uid = Yii::app()->user->id;
        }
        $criteria->compare('aid',$this->aid);
        $criteria->compare('title',$this->title,true);
        $criteria->compare('content',$this->content,true);
        $criteria->compare('img',$this->img,true);
        $criteria->compare('url',$this->url,true);
        $criteria->compare('cid',$this->cid);
        $criteria->compare('type',0);
        $criteria->compare('order',$this->order);
        $criteria->compare('enabled',$this->enabled);
        $criteria->compare('uid',$this->uid);
        $criteria->compare('longitude',$this->longitude,true);
        $criteria->compare('latitude',$this->latitude,true);
        $criteria->compare('ctime',$this->ctime);
        $criteria->compare('utime',$this->utime);

//        $criteria->addCondition("type=1");
//        $criteria->sort=array('defaultOrder'=>'ctiem DESC');
        return new CActiveDataProvider($this, array(
            'sort'=>array('defaultOrder'=>'t.ctime DESC'),
            'criteria'=>$criteria,
        ));
    }

    public function search_imgs()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;
        if(empty($this->uid) && (!Yii::app()->user->isSuperuser)){
            $this->uid = Yii::app()->user->id;
        }

        $criteria->compare('aid',$this->aid);
        $criteria->compare('title',$this->title,true);
        $criteria->compare('content',$this->content,true);
        $criteria->compare('img',$this->img,true);
        $criteria->compare('url',$this->url,true);
        $criteria->compare('cid',$this->cid);
        $criteria->compare('type',3);
        $criteria->compare('order',$this->order);
        $criteria->compare('enabled',$this->enabled);
        $criteria->compare('uid',$this->uid);
        $criteria->compare('longitude',$this->longitude,true);
        $criteria->compare('latitude',$this->latitude,true);
        $criteria->compare('ctime',$this->ctime);
        $criteria->compare('utime',$this->utime);

//        $criteria->addCondition("type=1");
//        $criteria->sort=array('defaultOrder'=>'ctiem DESC');
        return new CActiveDataProvider($this, array(
            'sort'=>array('defaultOrder'=>'t.ctime DESC'),
            'criteria'=>$criteria,
        ));
    }

    public function search_post()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;
        if(empty($this->uid) && (!Yii::app()->user->isSuperuser)){
            $this->uid = Yii::app()->user->id;
        }

        $criteria->compare('aid',$this->aid);
        $criteria->compare('title',$this->title,true);
        $criteria->compare('content',$this->content,true);
        $criteria->compare('img',$this->img,true);
        $criteria->compare('url',$this->url,true);
        $criteria->compare('cid',$this->cid);
        $criteria->compare('type',2);
        $criteria->compare('order',$this->order);
        $criteria->compare('enabled',$this->enabled);
        $criteria->compare('uid',$this->uid);
        $criteria->compare('longitude',$this->longitude,true);
        $criteria->compare('latitude',$this->latitude,true);
        $criteria->compare('ctime',$this->ctime);
        $criteria->compare('utime',$this->utime);

//        $criteria->addCondition("type=2");
//        $criteria->sort=array('defaultOrder'=>'ctiem DESC');
        return new CActiveDataProvider($this, array(
            'sort'=>array('defaultOrder'=>'t.ctime DESC'),
            'criteria'=>$criteria,
        ));
    }

    public function search_place()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;
        if(empty($this->uid) && (!Yii::app()->user->isSuperuser)){
            $this->uid = Yii::app()->user->id;
        }

        $criteria->compare('aid',$this->aid);
        $criteria->compare('title',$this->title,true);
        $criteria->compare('content',$this->content,true);
        $criteria->compare('img',$this->img,true);
        $criteria->compare('url',$this->url,true);
        $criteria->compare('cid',$this->cid);
        $criteria->compare('type',1);
        $criteria->compare('order',$this->order);
        $criteria->compare('enabled',$this->enabled);
        $criteria->compare('uid',$this->uid);
        $criteria->compare('longitude',$this->longitude,true);
        $criteria->compare('latitude',$this->latitude,true);
        $criteria->compare('ctime',$this->ctime);
        $criteria->compare('utime',$this->utime);

//        $criteria->addCondition("type=3");
//        $criteria->sort=array('defaultOrder'=>'ctiem DESC');
        return new CActiveDataProvider($this, array(
            'sort'=>array('defaultOrder'=>'t.ctime DESC'),
            'criteria'=>$criteria,
        ));
    }


}